const { log } = require("console");
const path = require("path");

const {
  fileUploadError,
  unSupportedFileType,
  publishTopicError,
  getTopicError,
  postCommentError,
  getCommentsError,
  getAllTopicsAndCommentsError,
  addLikesError,
  dislikesError,
  deleteTopicError,
} = require("../constant/err.type");

const {
  createTopic,
  getAllTopics,
  postMyComment,
  getComments,
  addLikes,
  disLikes,
  deleteComments,
  deleteTopic,
} = require("../service/topic.service");

class TopicController {
  async upload(ctx, next) {
    //? console.log(ctx.request.files.file);
    const { file } = ctx.request.files;
    //!console.log(file.type.substring(0, 5)); 校验文件是否为图片
    if (file.type.substring(0, 5) != "image") {
      return ctx.app.emit("error", unSupportedFileType, ctx);
    }
    /*   let file = ctx.request.body.data;
    console.log("body开始---------------------------");
    console.log(ctx.request.body);
    console.log("body结束---------------------------"); */
    /* console.log(path.basename(file.path)); */
    if (file) {
      ctx.body = {
        code: 0,
        message: "图片上传成功",
        result: {
          topic_img: "http://82.157.94.91:8000/" + path.basename(file.path),
        },
      };
    } else {
      return ctx.app.emit("error", fileUploadError, ctx);
    }
  }

  async create(ctx) {
    //! 直接调用service的createGoods方法
    try {
      console.log(ctx.request.body);
      const res = await createTopic(ctx.request.body);
      ctx.body = {
        code: 0,
        message: "发布帖子成功",
        result: res,
      };
    } catch (err) {
      console.log(err);
      return ctx.app.emit("error", publishTopicError, ctx);
    }
  }

  async getAll(ctx) {
    try {
      const res = await getAllTopics();
      ctx.body = {
        code: 0,
        message: "获取所有帖子信息成功",
        result: res,
      };
    } catch (err) {
      console.log(err);
      return ctx.app.emit("error", getTopicError, ctx);
    }
  }

  async postComment(ctx) {
    try {
      const res = await postMyComment(ctx.request.body);
      ctx.body = {
        code: 0,
        message: "发布帖子评论成功",
        result: res,
      };
    } catch (err) {
      console.log(err);
      return ctx.app.emit("error", postCommentError, ctx);
    }
  }

  async getTopicComent(ctx) {
    try {
      const res = await getComments(ctx.params.topic_id);
      return (ctx.body = {
        code: 0,
        message: "获取该帖子的评论信息成功",
        result: res,
      });
    } catch (err) {
      console.log(err);
      return ctx.app.emit("error", getCommentsError, ctx);
    }
  }

  async getAllTopicsAndComments(ctx) {
    try {
      let topics = await getAllTopics();
      let res = [...topics];

      for (let i = 0; i < topics.length; i++) {
        res[i]["dataValues"].comments_mes = [];
      }
      for (let i = 0; i < topics.length; i++) {
        let comments = await getComments(topics[i].id);

        for (let j = 0; j < comments.length; j++) {
          res[i]["dataValues"].comments_mes[j] = comments[j].dataValues;
        }
      }

      return (ctx.body = {
        code: 0,
        message: "获取所有的帖子和其评论信息成功",
        result: res,
      });
    } catch (err) {
      console.log(err);
      return ctx.app.emit("error", getAllTopicsAndCommentsError, ctx);
    }
  }

  async likePost(ctx) {
    try {
      let res = await addLikes(ctx.request.body.id);
      ctx.body = {
        code: 0,
        message: `点赞${ctx.request.body.id}号帖子成功`,
        result: res,
      };
    } catch (err) {
      console.log(err);
      return ctx.app.emit("error", addLikesError, ctx);
    }
  }

  async dislikePost(ctx) {
    try {
      let res = await disLikes(ctx.request.body.id);
      ctx.body = {
        code: 0,
        message: `取消点赞${ctx.request.body.id}号帖子成功`,
        result: res,
      };
    } catch (err) {
      console.log(err);
      return ctx.app.emit("error", dislikesError, ctx);
    }
  }

  async deleteMyTopic(ctx) {
    try {
      let topic_id = ctx.request.body.id;
      console.log("删除帖子的id", topic_id);
      await deleteComments(topic_id);
      let res = await deleteTopic(topic_id);
      ctx.body = {
        code: 0,
        message: `删除帖子id为${ctx.request.body.id}的正文及其所有评论成功`,
        result: res,
      };
    } catch (err) {
      console.log(err);
      return ctx.app.emit("error", deleteTopicError, ctx);
    }
  }
}

module.exports = new TopicController();
